unit uAddAgreement;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls, ComCtrls, ExtCtrls, Mask, DBCtrlsEh, DBLookupEh;

type
  TfAddAgreement = class(TForm)
    Label1: TLabel;
    Label2: TLabel;
    Label5: TLabel;
    DBLookupComboboxEh1: TDBLookupComboboxEh;
    DBLookupComboboxEh2: TDBLookupComboboxEh;
    DBLookupComboboxEh3: TDBLookupComboboxEh;
    Label3: TLabel;
    Panel1: TPanel;
    Bevel1: TBevel;
    Panel2: TPanel;
    Button1: TButton;
    Button2: TButton;
    DBEditEh1: TDBEditEh;
    DBDateTimeEditEh1: TDBDateTimeEditEh;
    DBEditEh2: TDBEditEh;
    Label4: TLabel;
    vPayment: TEdit;
    Label6: TLabel;
    Label7: TLabel;
    procedure Button2Click(Sender: TObject);
    procedure Button1Click(Sender: TObject);
    procedure vPaymentKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure vCountWindowKeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure DBLookupComboboxEh3EditButtons0Click(Sender: TObject;
      var Handled: Boolean);
    procedure DBEditEh2KeyDown(Sender: TObject; var Key: Word;
      Shift: TShiftState);
    procedure FormShow(Sender: TObject);
    procedure FormClose(Sender: TObject; var Action: TCloseAction);
    procedure DBEditEh2Change(Sender: TObject);
  private
    { Private declarations }
  public
    IsEditMode: Boolean;
    procedure FilterKeys(KeyCode: Integer);
    procedure ClearValues();
  end;

var
  fAddAgreement: TfAddAgreement;

implementation

uses uDBData, uDBCommand, uAddClient;

{$R *.dfm}

procedure TfAddAgreement.FilterKeys(KeyCode: Integer);
var
  tMSG: tagMSG;
begin
  if(((KeyCode >= 48) and (KeyCode <= 57)) or ((KeyCode >= 96) and (KeyCode <= 105)) or (KeyCode = 8) or (KeyCode = 188)) then
  begin
    PostMessage(Handle, WM_CHAR, 0, KeyCode);
  end else PeekMessage(tMSG, Handle, WM_CHAR, 0, PM_REMOVE);
end;

procedure TfAddAgreement.ClearValues();
begin
end;


procedure TfAddAgreement.Button2Click(Sender: TObject);
begin
  fDBData.dAgreement.DataSet.Cancel;
  Close();
end;

procedure TfAddAgreement.Button1Click(Sender: TObject);
begin
  fDBData.dAgreement.DataSet.FieldByName('WorkmanId').Value := 1;
  fDBData.dAgreement.DataSet.Post;
  if((vPayment.Text <> '')and(StrToFloat(vPayment.Text) > 0)and(not IsEditMode)) then
  begin
    fDBData.qAgreementPayment.Active := False;
    fDBData.qAgreementPayment.ParamByName('IDAgreement').Value := fDBData.qAgreement.InsertId;
    fDBData.qAgreementPayment.Active := True;
    fDBData.InsertRecord(fDBData.dAgreementPayment);
    fDBData.dAgreementPayment.DataSet.FieldByName('AgreementId').Value := fDBData.qAgreement.InsertId;
    fDBData.dAgreementPayment.DataSet.FieldByName('Summa').Value := StrToFloat(vPayment.Text);
    fDBData.dAgreementPayment.DataSet.FieldByName('AData').Value := DBDateTimeEditEh1.Value;
    fDBData.dAgreementPayment.DataSet.Post();
  end;
  if(not fDBData.qAgreementWindow.Active)then
  begin
    fDBData.qAgreementWindow.ParamByName('IDAgreement').Value := fDBData.qAgreement.InsertId;
    fDBData.qAgreementWindow.Active := True;
  end;
  Close();
end;

procedure TfAddAgreement.vPaymentKeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  FilterKeys(Key);
end;

procedure TfAddAgreement.vCountWindowKeyDown(Sender: TObject;
  var Key: Word; Shift: TShiftState);
begin
  FilterKeys(Key);
end;

procedure TfAddAgreement.DBLookupComboboxEh3EditButtons0Click(
  Sender: TObject; var Handled: Boolean);
begin
  fDBData.dClient.DataSet.Append;
  fAddClient.ShowModal();
end;

procedure TfAddAgreement.DBEditEh2KeyDown(Sender: TObject; var Key: Word;
  Shift: TShiftState);
begin
  FilterKeys(Key);
end;

procedure TfAddAgreement.FormShow(Sender: TObject);
begin
  if(IsEditMode)then
  begin
    vPayment.Enabled := False;
  end else
  begin
    vPayment.Enabled := True;
    vPayment.Text := '0';
  end;
end;

procedure TfAddAgreement.FormClose(Sender: TObject;
  var Action: TCloseAction);
begin
  IsEditMode := False;
end;

procedure TfAddAgreement.DBEditEh2Change(Sender: TObject);
begin
  if((Sender as TDBEditEh).Text <> '')then
  begin
    vPayment.Text := FloatToStr(StrToFloat((Sender as TDBEditEh).Text)/2);
  end;
end;

end.
